package com.example.myapplication.tools.webservice;

import android.content.Context;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WebServiceHelper {
	private final static int timeout = 30000; //默认超时时间为30s
	public final static int WS_ERROR=0;
	public final static int WS_NODATA=1;
	public final static int WS_SUCCEED=2;
	private static String WS_NAMESPACE;
	private static String WS_URL;
	private static boolean DOTNET;
	public static boolean ISInit=false;
	public static void init(String namespace,String url,boolean dotNet,Context context){
		if(!ISInit){
			WS_NAMESPACE=namespace;
			WS_URL=url;
			DOTNET=dotNet;
			ISInit=true;
		}
	}
	public static Object GetResponse(String method, List<SoapParam> params) {
		SoapObject request = new SoapObject(WS_NAMESPACE, method);
		if (params != null) {
			for (int i = 0, len = params.size(); i < len; i++) {
				request.addProperty(params.get(i).getmKey(), params.get(i).getmValue());
			}
		}
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = DOTNET;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(WS_URL,timeout);
		return getWSResult(androidHttpTransport,method,envelope);
	}

	public static Object InvokeMethod(String method) {
		SoapObject request = new SoapObject(WS_NAMESPACE, method);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = DOTNET;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(WS_URL,timeout);
		return getWSResult(androidHttpTransport,method,envelope);
	}
	
	public static Object InvokeMethod(String method, Object[] oo,int timeout) {
		SoapObject request = new SoapObject(WS_NAMESPACE, method);
		if (oo != null) {
			for (int i = 0, length = oo.length; i < length; i++) {
				request.addProperty("arg" + i, oo[i]);
			}
		}
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = DOTNET;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(WS_URL,timeout);
		return getWSResult(androidHttpTransport,method,envelope);
	}
	
	public static Object GetResponse(String method, List<SoapParam> params,int timeout) {
		SoapObject request = new SoapObject(WS_NAMESPACE, method);
		if (params != null) {
			for (int i = 0, len = params.size(); i < len; i++) {
				request.addProperty(params.get(i).getmKey(), params.get(i).getmValue());
			}
		}
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = DOTNET;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(WS_URL,timeout);
		return getWSResult(androidHttpTransport,method,envelope);
	}

	public static Object InvokeMethod(String method,int timeout) {
		SoapObject request = new SoapObject(WS_NAMESPACE, method);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = DOTNET;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(WS_URL,timeout);
		return getWSResult(androidHttpTransport,method,envelope);
	}
	public static Object InvokeMethod(String method, Object[] oo) {
		SoapObject request = new SoapObject(WS_NAMESPACE, method);
		if (oo != null) {
			for (int i = 0, length = oo.length; i < length; i++) {
				request.addProperty("arg" + i, oo[i]);
			}
		}
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = DOTNET;
		envelope.setOutputSoapObject(request);

		HttpTransportSE androidHttpTransport = new HttpTransportSE(WS_URL,timeout);
		return getWSResult(androidHttpTransport,method,envelope);
	}
	private static Object getWSResult(HttpTransportSE androidHttpTransport,String method,SoapSerializationEnvelope envelope) {
		try {
			ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
			headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
			androidHttpTransport.call(WS_NAMESPACE + method, envelope,headerPropertyArrayList);
		} catch (HttpResponseException e) {
			e.printStackTrace();
			return "Error";
		} catch (IOException e) {
			e.printStackTrace();
			return "Error";
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return "Error";
		}
		try {
			return envelope.getResponse();
		} catch (SoapFault e) {
			e.printStackTrace();
			return "Error";
		}
	}
}
